package com.gzz.orchid.main.facade.controller.example;

import com.alibaba.fastjson2.JSON;
import com.gzz.orchid.main.facade.controller.pojo.valid.UserReq;
import com.gzz.orchid.main.facade.serice.IValidationDemoService;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author higmd
 */
@Slf4j

@RestController
@RequestMapping("/example/valid")
public class ValidationController {
    @Autowired
    private IValidationDemoService validationDemoService;

    /**
     * 测试参数校验
     *
     * @param id   参数1
     * @param name 参数2
     * @return 结果
     */
    @RequestMapping("/validate")
    public String validate(@NotNull(message = "id不能为空") Integer id, @NotBlank(message = "name不能为空") String name) {
        return validationDemoService.validate(id, name);
    }

    /**
     * 测试参数校验
     *
     * @param id   参数1
     * @param name 参数2
     * @return 结果
     */
    @Validated
    @RequestMapping("/validate2")
    public String validate2(@NotNull(message = "id不能为空") Integer id, @NotBlank(message = "name不能为空") String name) {
        return (String.format("接收到数据：%d,   %s", id, name));
    }

    /**
     * 测试参数校验
     *
     * @param req 请求参数
     * @return 响应结果
     */
    @RequestMapping("/validatePojo")
    public String validatePojo(@Validated UserReq req) {
        return JSON.toJSONString(req);
    }
}
